from django.shortcuts import render, HttpResponse,redirect
from people import models

# Create your views here.
def index(request):
    print(1)
    if request.method == "POST":
        d_user = request.POST.get('user')
        d_pwd = request.POST.get('password')
        models.D_user.objects.create(d_user = d_user, d_pwd = d_pwd)

    user_list = models.D_user.objects.all()
    # print(user_list)
    return render(request, 'index.html', {'data': user_list})


def del_p(request, uid):
    # print(2)
    # print(request.GET.get('uid'))
    models.D_user.objects.filter(id=uid).delete()
    user_list = models.D_user.objects.all()
    return render(request, 'index.html',{'data': user_list})

def updata_p(request,uid):
    if request.method == "POST":
        d_user = request.POST.get('user')
        d_pwd = request.POST.get('password')
        models.D_user.objects.filter(id=uid).update(d_user = d_user, d_pwd = d_pwd)
        user_list = models.D_user.objects.all()
        return redirect('/', {'data': user_list})
    else:
        p =  models.D_user.objects.get(id=uid)
        print(p)
        return render(request, 'updata.html', {'data': p})